<?php

namespace app\admin\controller\mul;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Missionrebuy extends Backend
{
    
    /**
     * MulMissionReBuy模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('MulMissionReBuy');

    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where=array();
            $inputs=input();
            $input = json_decode($inputs['filter'], true);
            if(!empty($input['id'])){
                $where['id']=$input['id'];
            }
            if(!empty($input['user_name'])){
                $user_id=DB::name('users')->where(['nickname'=>array('like','%'.$input['user_name'].'%')])->value('user_id');
                $where['user_id']=array('in',$user_id);
            }
            if(!empty($input['mission_name'])){
                $employee=DB::name('MulMissionEmployee')->where(['company_name'=>array('like','%'.$input['mission_name'].'%')])->value('id');
                $where['mission_employee_id']=array('in',$employee);
            }
            if(!empty($input['id'])){
                $where['user_id']=$input['user_id'];
            }
            if(!empty($input['mission_employee_id'])){
                $where['mission_employee_id']=$input['mission_employee_id'];
            }
            if(!empty($input['status'])){
                $where['status']=$input['status'];
            }
            if(!empty($input['goods'])){
                $where['goods']=array('like','%'.$input['goods'].'%');
            }
            if(!empty($input['create_time'])) {
                $date = explode(',', $input['create_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['create_time'] = array('between', $date);
            }
            if(!empty($input['done_time'])) {
                $date = explode(',', $input['done_time']);
                foreach($date as &$v){
                    $v = strtotime($v);
                }
                $where['done_time'] = array('between', $date);
            }
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $data=array(
                '1'=>'已申请回购',
                '2'=>'金主已配货',
                '3'=>'已拒绝',
                '4'=>'过期关闭',
                '5'=>'已结账'
            );
            foreach($list as &$l){
                $l['status']=$data[$l['status']];
                $l['mission_name']=DB::name('MulMissionEmployee')->where('id='.$l['mission_employee_id'])->value('company_name');
                $l['user_name']=DB::name('Users')->where('user_id='.$l['user_id'])->value('nickname');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $btns = array('refresh');

        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }

}
